// #include "widget.h"

// Widget::Widget(QWidget *parent)
//     : QWidget(parent)
// {
//     ui.setupUi(this);
// }

// Widget::~Widget() {}


#include "widget.h"
#include <QRegularExpression>
#include <QStringList>
#include <QClipboard>
#include <QApplication>
#include <QWidget>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle("文本格式化工具");
    setMinimumSize(600, 500);
    
    // 创建中心部件
    QWidget *centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);
    
    // 主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);
    
    // 输入区域
    QLabel *inputLabel = new QLabel("输入内容:", this);
    mainLayout->addWidget(inputLabel);
    
    QHBoxLayout *inputLayout = new QHBoxLayout();
    inputTextEdit = new QTextEdit(this);
    inputTextEdit->setPlaceholderText("请输入需要格式化的内容，支持空格、逗号或换行分隔...");
    inputCopyBtn = new QPushButton("复制", this);
    
    inputLayout->addWidget(inputTextEdit);
    inputLayout->addWidget(inputCopyBtn);
    mainLayout->addLayout(inputLayout);
    
    // 输出区域 - 逗号分隔
    QLabel *commaLabel = new QLabel("逗号分隔格式:", this);
    mainLayout->addWidget(commaLabel);
    
    QHBoxLayout *commaLayout = new QHBoxLayout();
    commaOutputEdit = new QTextEdit(this);
    commaOutputEdit->setReadOnly(true);
    commaCopyBtn = new QPushButton("复制", this);
    
    commaLayout->addWidget(commaOutputEdit);
    commaLayout->addWidget(commaCopyBtn);
    mainLayout->addLayout(commaLayout);
    
    // 输出区域 - 空格分隔
    QLabel *spaceLabel = new QLabel("空格分隔格式:", this);
    mainLayout->addWidget(spaceLabel);
    
    QHBoxLayout *spaceLayout = new QHBoxLayout();
    spaceOutputEdit = new QTextEdit(this);
    spaceOutputEdit->setReadOnly(true);
    spaceCopyBtn = new QPushButton("复制", this);
    
    spaceLayout->addWidget(spaceOutputEdit);
    spaceLayout->addWidget(spaceCopyBtn);
    mainLayout->addLayout(spaceLayout);
    
    // 输出区域 - 换行分隔
    QLabel *lineLabel = new QLabel("换行分隔格式:", this);
    mainLayout->addWidget(lineLabel);
    
    QHBoxLayout *lineLayout = new QHBoxLayout();
    lineOutputEdit = new QTextEdit(this);
    lineOutputEdit->setReadOnly(true);
    lineCopyBtn = new QPushButton("复制", this);
    
    lineLayout->addWidget(lineOutputEdit);
    lineLayout->addWidget(lineCopyBtn);
    mainLayout->addLayout(lineLayout);
    
    // 添加伸缩项，使控件上方对齐
    mainLayout->addStretch();
    
    // 连接信号槽
    connect(inputTextEdit, &QTextEdit::textChanged, this, &MainWindow::processText);
    connect(inputCopyBtn, &QPushButton::clicked, [this]() {
        copyToClipboard(inputTextEdit);
    });
    connect(commaCopyBtn, &QPushButton::clicked, [this]() {
        copyToClipboard(commaOutputEdit);
    });
    connect(spaceCopyBtn, &QPushButton::clicked, [this]() {
        copyToClipboard(spaceOutputEdit);
    });
    connect(lineCopyBtn, &QPushButton::clicked, [this]() {
        copyToClipboard(lineOutputEdit);
    });
}

MainWindow::~MainWindow()
{
    // Qt的父对象机制会自动释放子控件，无需手动删除
}

// 解析输入文本，识别各种分隔符
QStringList MainWindow::parseInput(const QString &input)
{
    QStringList items;
    
    // 使用正则表达式分割各种可能的分隔符：逗号、空格、换行
    // 同时处理多个连续分隔符的情况
    QStringList parts = input.split(QRegularExpression("[, \\n\\r]+"), Qt::SkipEmptyParts);
    
    // 去重处理（可选，如果需要保留重复项可以注释掉）
    foreach (QString part, parts) {
        if (!part.trimmed().isEmpty() && !items.contains(part.trimmed())) {
            items.append(part.trimmed());
        }
    }
    
    return items;
}

// 处理文本并更新输出
void MainWindow::processText()
{
    QString input = inputTextEdit->toPlainText();
    QStringList items = parseInput(input);
    
    // 更新各个输出框
    commaOutputEdit->setText(items.join(","));
    spaceOutputEdit->setText(items.join(" "));
    lineOutputEdit->setText(items.join("\n"));
}

// 复制文本到剪贴板
void MainWindow::copyToClipboard(QTextEdit* textEdit)
{
    if (!textEdit->toPlainText().isEmpty()) {
        QClipboard *clipboard = QApplication::clipboard();
        clipboard->setText(textEdit->toPlainText());
    }
    QMessageBox::information(this,QString("提示"),QString("复制成功!"));
}
